package com.mayikt.service.impl;


import com.mayikt.base.BaseApiService;
import com.mayikt.entity.PaymentChannelEntity;
import com.mayikt.mapper.PaymentChannelMapper;
import com.mayikt.service.PayCallbackService;
import com.mayikt.template.AbstractPayCallbackTemplate;
import com.mayikt.utils.SpringContextUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayCallbackServiceImpl extends BaseApiService implements PayCallbackService {
    @Autowired
    private PaymentChannelMapper paymentChannelMapper;

    @Override
    public Object asynCallback(String channelId) {
        if (StringUtils.isEmpty(channelId)) {
            return setResultError("channelId不能为空");
        }
        // 1.根据渠道id查询渠道信息
        PaymentChannelEntity pce = paymentChannelMapper.selectBychannelId(channelId);
        if (pce == null) {
            return setResultError("该渠道已关闭或不存在，请联系管理员");
        }
        //2.获取模板的baenid
        String callbackBeanId = pce.getCallbackBeanId();
        if (StringUtils.isEmpty(callbackBeanId)) {
            return setResultError("没有配置callbackBeanId");
        }
        //3.直接从Spring容器上下文获取
        AbstractPayCallbackTemplate payCallbackTemplate =
                SpringContextUtils.getBean(callbackBeanId, AbstractPayCallbackTemplate.class);
        String result = payCallbackTemplate.asyncCallBack(pce);
        return result;
    }
}
